package com.heihei.serviceimpl.res;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heihei.common.page.Condition;
import com.heihei.common.page.Query;
import com.heihei.common.response.ResponseVO;
import com.heihei.entity.res.ResTagEntity;
import com.heihei.enums.DeleteFlagEnum;
import com.heihei.mapper.res.ResTagMapper;
import com.heihei.service.res.ResTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author duanhengfei
 * @since 2021-11-23
 */
@Service
public class ResTagServiceImpl extends ServiceImpl<ResTagMapper, ResTagEntity> implements ResTagService {

    /**
     * 添加
     *
     * @author duanhf
     * 2021年11月25日 11:20:25
     */
    @Override
    public ResponseVO<String> add(ResTagEntity paramEntity) {
        paramEntity.setId(null);
        if(StringUtils.isEmpty(paramEntity.getTagName())) {
            //提示错误信息
            return ResponseVO.paramLess("标签名称不能为空");
        }
        //查询一下，保证标签名称不能重复（如果已经有重复的，则提示有重复）
        LambdaQueryWrapper<ResTagEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResTagEntity::getTagName, paramEntity.getTagName());
        List<ResTagEntity> dataList = this.baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(dataList)) {
            return ResponseVO.paramIllegal("已经有重名的标签");
        }
        this.save(paramEntity);
        return ResponseVO.dealSuccess(paramEntity.getId());
    }

    /**
     * 选择性添加标签信息（返回这些标签的id集合）
     *
     * @author duanhf
     * 2021年12月07日 15:07:11
     */
    @Override
    public List<String> addSelective(List<ResTagEntity> tagList) {
        List<String> tagNameList = tagList.stream().map(ResTagEntity::getTagName)
                .distinct().collect(Collectors.toList());
        //标签是要求不能重名的
        LambdaQueryWrapper<ResTagEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ResTagEntity::getTagName, tagNameList);
        List<ResTagEntity> currTagList = this.list(queryWrapper);
        List<String> currNameList = currTagList.stream().map(ResTagEntity::getTagName).collect(Collectors.toList());

        tagNameList.removeAll(currNameList);
        List<String> newIdList = null;
        if(CollectionUtils.isNotEmpty(tagNameList)) {
            List<ResTagEntity> newTagEntityList = tagNameList.stream().distinct().map(tagName -> {
                ResTagEntity ent = new ResTagEntity();
                ent.setTagName(tagName);
                return ent;
            }).collect(Collectors.toList());
            this.saveBatch(newTagEntityList);
            newIdList = newTagEntityList.stream().map(ResTagEntity::getId).collect(Collectors.toList());
        }

        List<String> currIdList = currTagList.stream().map(ResTagEntity::getId).collect(Collectors.toList());
        List<String> resultIdList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(currIdList)) {
            resultIdList.addAll(currIdList);
        }
        if(CollectionUtils.isNotEmpty(newIdList)) {
            resultIdList.addAll(newIdList);
        }

        return resultIdList;
    }

    /**
     * 根据id进行修改
     *
     * @author duanhf
     * 2021年11月25日 20:56:08
     */
    @Override
    public ResponseVO<Boolean> myUpdateById(ResTagEntity paramEntity) {
        if(StringUtils.isEmpty(paramEntity.getId())) {
            return ResponseVO.paramLess("id不能为空");
        }
        boolean flag = this.updateById(paramEntity);
        return ResponseVO.dealSuccess(flag);
    }

    /**
     * 删除（逻辑删除）
     *
     * @author duanhf
     * 2021年11月25日 22:13:23
     */
    @Override
    public ResponseVO<Boolean> deleteByIdLogic(String id) {
        if(StringUtils.isEmpty(id)) {
            return ResponseVO.paramLess("id不能为空");
        }
        ResTagEntity paramEntity = new ResTagEntity();
        paramEntity.setId(id);
        paramEntity.setDelFlag(DeleteFlagEnum.DELETED.getKey());
        //执行逻辑删除操作
        boolean flag = this.updateById(paramEntity);
        return ResponseVO.dealSuccess(flag);
    }

    /**
     * 批量删除
     *
     * @author duanhf
     * 2021年11月26日 10:26:17
     */
    @Override
    public ResponseVO<Boolean> deleteBatch(List<String> idList) {
        if(CollectionUtils.isEmpty(idList)) {
            return ResponseVO.paramLess("请传入要删除记录的主键ID");
        }
        //注：removeByIds是真删除
//        boolean flag = this.removeByIds(idList);
        //这里需要假删除（逻辑删除）

        List<ResTagEntity> entityList = idList.stream().map(id -> {
            ResTagEntity tag = new ResTagEntity();
            tag.setId(id);
            tag.setDelFlag(DeleteFlagEnum.DELETED.getKey());
            return tag;
        }).collect(Collectors.toList());
        boolean flag = this.updateBatchById(entityList);

        return ResponseVO.dealSuccess(flag);
    }

    /**
     * 列表查询（可以根据标签名字模糊检索）
     *
     * @author duanhf
     * 2021年11月26日 10:14:13
     */
    @Override
    public ResponseVO<IPage<ResTagEntity>> myPageList(ResTagEntity entityParam, Query query) {
        LambdaQueryWrapper<ResTagEntity> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(entityParam.getTagName())) {
            queryWrapper.like(ResTagEntity::getTagName, entityParam.getTagName());
        }
        //只查询未删除的数据
        queryWrapper.eq(ResTagEntity::getDelFlag, DeleteFlagEnum.NOT_DELETED.getKey());
        //分页查询
        IPage<ResTagEntity> pageDataList = this.page(Condition.getPage(query), queryWrapper);
        //返回结果
        return ResponseVO.dealSuccess(pageDataList);
    }

    /**
     * 查询详情
     *
     * @author duanhf
     * 2021年11月26日 10:22:50
     */
    @Override
    public ResponseVO<ResTagEntity> myGetById(String id) {
        ResTagEntity result = this.getById(id);
        if(result == null) {
            return ResponseVO.recordNotExists();
        }
        return ResponseVO.dealSuccess(result);
    }
}
